const app = getApp()
const INTERVAL = 10000;
Component({

  behaviors: [],

  properties: {
    myProperty: { // 属性名
      type: String,
      value: ''
    },
    myProperty2: String, // 简化的定义方式
    nav: 0,
    shenfen11: '1'
  },

  data: {
    appurl: app.apiurl,
    apiimg: app.apiimg,
    list: [],
    baoming1: false,
    baoming2: false,
    baoming3: false,
    baoming4: false,
    baoming6: false,
    baoming7: false,
    array: ['不限', '男性', '女性'],
    idx: 0,
    idx1: 0,
    array2: ['不限', '胜率高', '好评多'],
    idx2: 0,
    yonghu: '222',
    xiaoxi: 0,
    xiaoxi2: 0,
    baoming5: false,
    bm_tishi1: false,
    kh_info: false,
    kftanchuang2: false,
    kefu_ewm: ''
  }, // 私有数据，可用于模板渲染
  lifetimes: {
    ready() {

    },
    // 生命周期函数，可以为函数，或一个在 methods 段中定义的方法名
    attached: function () {
      let that = this
      this.timer = setInterval(function () {
        that.z_zhuangtai()
        // 这里可以执行定时器需要执行的任务
      }, INTERVAL);
    },
    moved: function () {},
    detached: function () {
      if (this.timer) {
        clearInterval(this.timer);
        this.timer = null; // 清除引用，便于垃圾回收
      }
    },
  },

  // 生命周期函数，可以为函数，或一个在 methods 段中定义的方法名
  attached: function () {}, // 此处 attached 的声明会被 lifetimes 字段中的声明覆盖
  ready: function () {},

  pageLifetimes: {
    // 组件所在页面的生命周期函数
    show: function () {
      this.xcx_msg_info1()
      this.z_peizhi()
      this.z_meiri()
      this.z_yonghu()
      this.z_baoming5()
      this.z_jilu4()
      this.setData({
        bottomHeight: app.globalData.bottomHeight
      })
      // this.z_zhuangtai() 
      this.z_idxgg()
    },
    hide: function () {},
    resize: function () {},
  },

  methods: {
    z_idxgg() {
      let _this = this
      let params = {
        "token": wx.getStorageSync("user_token"),
      }
      wx.request({
        url: app.apiurl + 'api/home/index',
        method: 'POST',
        data: params,
        header: {
          'content-type': 'application/x-www-form-urlencoded' // 修改请求
        },
        success(res) {
          if (res.data.data.kh_info != '' && res.data.data.kh_info.diff_minute != '0') {
            _this.setData({
              kh_info: true
            })
          } else {
            _this.setData({
              kh_info: false
            })
          }
        }
      })
    },
    z_zhuangtai() {
      let _this = this
      let params = {
        "token": wx.getStorageSync("user_token"),
      }
      wx.request({
        url: app.apiurl + 'api/compete/getMatchStatus',
        method: 'GET',
        data: params,
        header: {
          'content-type': 'application/x-www-form-urlencoded' // 修改请求
        },
        success(res) {
          if (res.data.code == '1001') {
            _this.setData({
              zhuangtai: res.data.data
            })
            if (res.data.data.bmstatus == '0') {
              wx.setStorageSync("baoming", '')
              _this.z_baoming5();
            } else {
              wx.setStorageSync("baoming", 'true')
              _this.z_baoming5();
            }
          }
        }
      })
    },
    z_peizhi() {
      let _this = this
      let params = {
        "token": wx.getStorageSync("user_token"),
      }
      wx.request({
        url: app.apiurl + 'api/config/configBase',
        method: 'GET',
        data: params,
        header: {
          'content-type': 'application/x-www-form-urlencoded' // 修改请求
        },
        success(res) {
          _this.setData({
            peizhi: res.data.data
          })
        }
      })
    },
    z_meiri() {
      let _this = this
      let params = {
        "token": wx.getStorageSync("user_token"),
      }
      wx.request({
        url: app.apiurl + 'api/member/todayBm2',
        method: 'POST',
        data: params,
        header: {
          'content-type': 'application/x-www-form-urlencoded' // 修改请求
        },
        success(res) {
          _this.setData({
            list: res.data.data
          })
        }
      })
    },
    z_open_dtly() {
      wx.navigateTo({
        url: '/packageA/pages/kaihei/kaihei?id=0',
      })
    },
    z_open() {
      this.setData({
        baoming1: true
      })
    },
    z_wancheng1() {
      let _this = this
      let params = {
        "token": wx.getStorageSync("user_token"),
        "up_field": 'hdy_status'
      }
      wx.request({
        url: app.apiurl + 'api/member/upNewUserGuide',
        method: 'POST',
        data: params,
        header: {
          'content-type': 'application/x-www-form-urlencoded' // 修改请求
        },
        success(res) {

        }
      })
    },
    z_open1() {
      this.setData({
        baoming2: true
      })
    },

    z_open_kefu() {
      let _this = this
      let params = {
        "token": wx.getStorageSync("user_token"),
      }
      wx.request({
        url: app.apiurl + 'api/danye/info?id=21',
        method: 'GET',
        data: params,
        header: {
          'content-type': 'application/x-www-form-urlencoded' // 修改请求
        },
        success(res) {
          _this.setData({
            kefu_ewm: res.data.data.pic
          })
        }
      })
      this.setData({
        kftanchuang2: true
      })
    },
    z_fabuzhuye(){
      let _this = this
      if (wx.getStorageSync("user_token") == "") {
        _this.z_dengluyanzeng()
      } else {
        let params = {
          "token":wx.getStorageSync("user_token"),
        }
        wx.request({
          url: app.apiurl + 'api/member/homePageCheck',
          method: 'GET',
          data: params,
          header: {
            'content-type': 'application/x-www-form-urlencoded' // 修改请求
          },
          success(res) {
            if(res.data.code == '1001'){
              wx.navigateTo({
                url: '/packageA/pages/home/home?id=0',
              })
            } else {
              wx.showToast({
                title: res.data.msg,
                icon: 'none',
                duration: 2000
              })
            }
          }
        })
      }
    },
    z_dengluyanzeng(){
      wx.request({
        url: app.apiurl + 'api/home/xcxLoginStatus',
        method: 'GET',
        data: {},
        header: {
          'content-type': 'application/x-www-form-urlencoded' // 修改请求
        },
        success(res) {
          if (res.data.code == '1001') {
            if (res.data.data.is_force_login == '1') {
              // wx.reLaunch({
              //   url: '/pages/login/login'
              // });
              wx.showToast({
                title: '请注册/登录会员账号',
                icon: 'none',
                duration: 2000
              })
              setTimeout(function(){
                wx.reLaunch({
                  url: '/pages/member/member',
                })
              },2500)
            }
          }
        }
      })
    },
    z_close_kefu() {
      this.setData({
        kftanchuang2: false
      })
    },
    z_open2() {
      this.setData({
        baoming3: true
      })
    },
    z_open3() {
      this.setData({
        baoming4: true
      })
    },
    z_open4() {
      let _this = this
      let params = {
        "token": wx.getStorageSync("user_token"),
        "yd_status": '2'
      }
      wx.request({
        url: app.apiurl + 'api/member/upMyYd',
        method: 'POST',
        data: params,
        header: {
          'content-type': 'application/x-www-form-urlencoded' // 修改请求
        },
        success(res) {
          _this.setData({
            baoming1: true
          })
        }
      })
    },
    z_open5() {
      this.setData({
        baoming1: false
      })
      this.z_yonghu()

      const pages = getCurrentPages();
      const currentPage = pages[pages.length - 1]; // 当前页面实例
      const homePagePath = '/pages/index/index'; // 首页路径（需与实际路径一致）

      // 判断当前是否在首页
      if (currentPage.route !== homePagePath) {
        // 不在首页时跳转
        wx.reLaunch({
          url: homePagePath,
          success: () => console.log('跳转首页成功'),
          fail: (err) => console.error('跳转失败:', err)
        });
      }
    },
    z_close() {
      this.setData({
        baoming1: false,
        baoming2: false,
        baoming3: false,
        baoming4: false,
        baoming6: false
      })
    },
    z_close1() {
      this.setData({
        bm_tishi1: false
      })
    },
    z_close2() {
      this.setData({
        baoming7: false
      })
      this.z_wancheng1()
    },
    bindPickerChange: function (e) {
      console.log('picker发送选择改变，携带值为', e.currentTarget.dataset.index)
      this.setData({
        idx: e.currentTarget.dataset.index
      })
    },
    bindPickerChange1: function (e) {
      console.log('picker发送选择改变，携带值为', e.currentTarget.dataset.index)
      this.setData({
        idx1: e.currentTarget.dataset.index
      })
    },
    bindPickerChange3: function (e) {
      console.log('picker发送选择改变，携带值为', e.currentTarget.dataset.index)
      if (e.currentTarget.dataset.index == 1) {
        this.setData({
          idx1: e.currentTarget.dataset.index,
          idx: 0,
          idx2: 0
        })
      } else {
        this.setData({
          idx1: e.currentTarget.dataset.index
        })
      }
    },
    bindPickerChange2: function (e) {
      console.log('picker发送选择改变，携带值为', e.currentTarget.dataset.index)
      this.setData({
        idx2: e.currentTarget.dataset.index
      })
    },
    z_baoming1() {
      let _this = this
      let params = {
        "token": wx.getStorageSync("user_token"),
        "other_sex": _this.data.array[_this.data.index],
        "type": "2"
      }
      wx.request({
        url: app.apiurl + 'api/compete/khBm',
        method: 'POST',
        data: params,
        header: {
          'content-type': 'application/x-www-form-urlencoded' // 修改请求
        },
        success(res) {
          // wx.showToast({
          //   title: res.data.msg,
          //   icon: 'success',
          //   duration: 2000
          // })
          _this.z_close()
          _this.z_yonghu()
          _this.z_baoming5()
        }
      })
    },
    z_baoming2() {
      let _this = this
      let params = {
        "token": wx.getStorageSync("user_token"),
      }
      wx.request({
        url: app.apiurl + 'api/compete/khQxBm',
        method: 'POST',
        data: params,
        header: {
          'content-type': 'application/x-www-form-urlencoded' // 修改请求
        },
        success(res) {
          // wx.showToast({
          //   title: res.data.msg,
          //   icon: 'success',
          //   duration: 2000
          // })
          _this.z_close()
          _this.z_yonghu()
          _this.setData({
            baoming5: false
          })
          wx.setStorageSync("baoming", '')
          _this.z_baoming5()
          // _this.z_idxgg()
        }
      })
    },
    z_baoming3() {
      let _this = this
      let params = {
        "token": wx.getStorageSync("user_token"),
        "other_sex": _this.data.array[_this.data.index],
        "type": "1"
      }
      wx.request({
        url: app.apiurl + 'api/compete/khBm',
        method: 'POST',
        data: params,
        header: {
          'content-type': 'application/x-www-form-urlencoded' // 修改请求
        },
        success(res) {
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
            duration: 2000
          })
          _this.z_close()
          _this.z_yonghu()
        }
      })
    },
    z_baoming4() {
      let _this = this
      let params = {
        "token": wx.getStorageSync("user_token")
      }
      wx.request({
        url: app.apiurl + 'api/compete/xyTzBm',
        method: 'POST',
        data: params,
        header: {
          'content-type': 'application/x-www-form-urlencoded' // 修改请求
        },
        success(res) {
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
            duration: 2000
          })
          _this.z_close()
          _this.z_yonghu()
        }
      })
    },
    z_baoming6() {
      let _this = this
      let jinbi_num
      if (_this.data.idx1 == 0) {
        jinbi_num = _this.data.peizhi.config_param23
      } else {
        jinbi_num = '0'
      }
      let other_sex
      if (_this.data.idx == 1) {
        other_sex = '男'
      } else if (_this.data.idx == 2) {
        other_sex = '女'
      } else {
        other_sex = ''
      }
      let other_win_rate
      if (_this.data.idx2 == 1) {
        other_win_rate = 1
      } else {
        other_win_rate = ''
      }
      let haoping_high
      if (_this.data.idx2 == 2) {
        haoping_high = 1
      } else {
        haoping_high = ''
      }
      let params = {
        "token": wx.getStorageSync("user_token"),
        "jinbi_num": jinbi_num,
        "other_sex": other_sex,
        "other_win_rate": other_win_rate,
        "haoping_high": haoping_high
      }
      wx.request({
        url: app.apiurl + 'api/compete/khBm',
        method: 'POST',
        data: params,
        header: {
          'content-type': 'application/x-www-form-urlencoded' // 修改请求
        },
        success(res) {
          if (res.data.code == '1001' || res.data.code == '1002') {
            wx.showToast({
              title: res.data.msg,
              icon: 'none',
              duration: 2000
            })
          } else {
            if (jinbi_num != 0) {
              _this.setData({
                bm_tishi1: true,
                bm_tishi2: res.data.msg
              })
            }
          }
          _this.setData({
            baoming5: true
          })
          wx.setStorageSync("baoming", 'true')
          _this.z_baoming5()
          wx.setStorageSync("pipei", 'true')
          _this.z_close()
          _this.z_yonghu()
          //  _this.z_idxgg()
          //  if(_this.data.zhuangtai.bmstatus == "2"){
          //    wx.navigateTo({
          //      url: '/packageA/pages/liao/liao.js?id=' + _this.data.zhuangtai.member_id,
          //    })
          //  }
        }
      })
    },
    z_baoming7() {
      let _this = this
      let params = {
        "token": wx.getStorageSync("user_token"),
        "id": wx.getStorageSync("kaiheiid")
      }
      wx.request({
        url: app.apiurl + 'api/compete/tuchuKh',
        method: 'POST',
        data: params,
        header: {
          'content-type': 'application/x-www-form-urlencoded' // 修改请求
        },
        success(res) {
          _this.z_close()
          _this.z_yonghu()
          wx.setStorageSync("baoming", '')
          _this.z_baoming5()
          // _this.z_idxgg()
        }
      })
    },
    z_yonghu() {
      let _this = this
      let params = {
        "token": wx.getStorageSync("user_token"),
      }
      wx.request({
        url: app.apiurl + 'api/member/myInfo',
        method: 'POST',
        data: params,
        header: {
          'content-type': 'application/x-www-form-urlencoded' // 修改请求
        },
        success(res) {
          _this.setData({
            login: true,
            yonghu: res.data.data
          })
        }
      })
    },
    xcx_msg_info1() {
      let _this = this
      let params = {
        "token": wx.getStorageSync("user_token"),
      }
      wx.request({
        url: app.apiurl + 'api/member/totalNoReadNum',
        method: 'POST',
        data: params,
        header: {
          'content-type': 'application/x-www-form-urlencoded' // 修改请求
        },
        success(res) {
          _this.setData({
            xiaoxi: res.data.data.total_num
          })
        }
      })
    },
    z_baoming5() {
      let _this = this
      if (wx.getStorageSync("baoming") == 'true') {
        _this.setData({
          baoming5: true
        })
      } else {
        _this.setData({
          baoming5: false
        })
      }
    },
    z_jilu4() {
      let _this = this
      let params = {
        "token": wx.getStorageSync("user_token")
      }
      wx.request({
        url: app.apiurl + 'api/member_tj/tjBmStatus',
        method: 'GET',
        data: params,
        header: {
          'content-type': 'application/x-www-form-urlencoded' // 修改请求
        },
        success(res) {
          _this.setData({
            bm_status: res.data.data.bm_status
          })
        }
      })
    },
    onMyButtonTap: function () {
      this.setData({
        // 更新属性和数据的方法与更新页面数据的方法类似
      })
    },
    // 内部方法建议以下划线开头
    _myPrivateMethod: function () {
      // 这里将 data.A[0].B 设为 'myPrivateData'
      this.setData({
        'A[0].B': 'myPrivateData'
      })
    },
    _propertyChange: function (newVal, oldVal) {

    }
  }

})